package com.yang.job.infra.event;


import com.yang.job.infra.utils.SpringContextUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.Ordered;

public class EventBusContext implements ApplicationListener<ApplicationContextEvent>, Ordered {
    private static EventBusContext instance;

    private ApplicationContextEvent applicationContextEvent;

    @Override
    public void onApplicationEvent(ApplicationContextEvent event) {
        if (event instanceof ContextRefreshedEvent) {
            System.out.println("刷新了==============");
            EventBusContext.instance = this;
            instance.applicationContextEvent = applicationContextEvent;
        } else if (event instanceof ContextClosedEvent) {
            System.out.println("销毁了==============");
            EventCenter eventcenter = SpringContextUtils.getBeanOfType(EventCenter.class);
            eventcenter.shutdown();
        }
    }

    @Override
    public int getOrder() {
        return 30;
    }
}

